package com.none.web.controller.like;



import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



import org.springframework.web.bind.annotation.ResponseBody;

import spring.mvc.bind.annotation.FormModel;

import com.none.core.common.utils.JSONUtil;
import com.none.core.model.Result;
import com.none.web.common.SysConstant;
import com.none.web.model.TLike;
import com.none.web.service.like.ILikeService;

/**
 * 点赞管理Controller
 * 
 * @author Summer
 * 
 */
@Controller
@RequestMapping("like")
public class LikeController {

	@Resource(name = "likeService")
	ILikeService likeService;

	@Autowired
	private HttpServletRequest request;
	
	/**
	 * 添加点赞信息
	 * 
	 * @param like
	 * @param type
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("saveLike")
	public Object saveLikeInfo(@FormModel("like") final TLike like, String type,String userId) throws Exception {
		String[] filter = new String[] {};

		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}

		Map<String, Object> data = new HashMap<String, Object>();
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);

		data.put("likeObj", likeService.saveLikeInfo(like, type, userId));
		return JSONUtil.toJSON(result, filter);
	}
	/**
	 * 取消点赞
	 * 
	 * @param contentId
	 * @param flag
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("cancleLike")
	public Object deleteLikeInfo(String contentId,String type,String userId) throws Exception {


		if(userId==null||userId.equals("")){
			userId = request.getHeader("BasicAuthUsername");
		}
		
		Map<String, Object> data = new HashMap<String, Object>();
		likeService.deleteLikeInfo(contentId, type,userId);
		Result result = new Result(SysConstant.STATE_SUCCESS, "golbal.success", data);
		return JSONUtil.toJSON(result);
	}
}
